package com.jdz.util;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by geely
 */
public class DateTimeUtil {

    //joda-time

    //str->Date
    //Date->str
    public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";



    public static Date strToDate(String dateTimeStr,String formatStr){
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }

    public static String dateToStr(Date date,String formatStr){
        if(date == null){
            return StringUtils.EMPTY;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(formatStr);
    }

    public static Date strToDate(String dateTimeStr){
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }

    public static String dateToStr(Date date){
        if(date == null){
            return StringUtils.EMPTY;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(STANDARD_FORMAT);
    }

    /**
     * 把日期变成今天最小日期
     * @param startTime
     * @return
     */
    public static Date dateMin(String startTime){

        Date startTimeDate = new Date();


        startTimeDate = DateTimeUtil.strToDate(startTime,"yyyy-MM-dd");


        Calendar ca1 = Calendar.getInstance();
        ca1.setTime(startTimeDate);
        ca1.set(Calendar.MILLISECOND,ca1.getActualMinimum(Calendar.MILLISECOND));
        startTimeDate=ca1.getTime();

        return startTimeDate;

    }

    /**
     * 把日期变成今天最大日期
     * @param stopTime
     * @return
     */
    public static Date dateMax(String stopTime){

        Date stopTimeDate = new Date();

        stopTimeDate = DateTimeUtil.strToDate(stopTime,"yyyy-MM-dd");

        Calendar ca2 = Calendar.getInstance();
        ca2.setTime(stopTimeDate);
        //设置每天的最大小时
        ca2.set(Calendar.HOUR_OF_DAY, ca2.getActualMaximum(Calendar.HOUR_OF_DAY));
        //设置每小时最大分钟
        ca2.set(Calendar.MINUTE, ca2.getActualMaximum(Calendar.MINUTE));
        //设置每分钟最大秒
        ca2.set(Calendar.SECOND, ca2.getActualMaximum(Calendar.SECOND));
        //设置每分钟最大毫秒
        ca2.set(Calendar.MILLISECOND,ca2.getActualMaximum(Calendar.MILLISECOND));
        stopTimeDate=ca2.getTime();
        return stopTimeDate;
    }


    /**
     * 判断时间是不是今天
     * @param date
     * @return    是返回true，不是返回false
     */
    public static boolean isNow(Date date) {
        //当前时间
        Date now = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        //获取今天的日期
        String nowDay = sf.format(now);


        //对比的时间
        String day = sf.format(date);

        return day.equals(nowDay);

    }

    //判断又没超过24小时
    public static boolean judgmentDate(String date1, String date2) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date start = null;
        Date end = null;
        try {
            start = sdf.parse(date1);
            end = sdf.parse(date2);
        } catch (ParseException e) {
            e.printStackTrace();
        }



        long cha = end.getTime() - start.getTime();

        if(cha<0){

            return false;

        }

        double result = cha * 1.0 / (1000 * 60 * 60);

        if(result<=24){

            return true;

        }else{

            return false;

        }

    }

    //判断时间格式对不
    public static boolean checkDate(String date,String format) {
        DateFormat df = new SimpleDateFormat(format);
        Date d = null;
        try{
            d = df.parse(date);
        }catch(Exception e){
            //如果不能转换,肯定是错误格式
            return false;
        }
        String s1 = df.format(d);
        // 转换后的日期再转换回String,如果不等,逻辑错误.如format为"yyyy-MM-dd",date为
        // "2006-02-31",转换为日期后再转换回字符串为"2006-03-03",说明格式虽然对,但日期
        // 逻辑上不对.
        return date.equals(s1);
    }


//    public static void main(String[] args) {
//        System.out.println(DateTimeUtil.dateToStr(new Date(),"yyyy-MM-dd HH:mm:ss"));
//        System.out.println(DateTimeUtil.strToDate("2010-01-01 11:11:11","yyyy-MM-dd HH:mm:ss"));
//
//    }
public static void main(String[] args)
{
//    int [] data = {1,2,3,4,5,6,7,8,9};
//
//    List a = new ArrayList();
//    a.add(1);
//    a.add(2);
//    a.add(3);
//    a.add(4);
//    a.add(5);
//    a.add(6);
//    a.add(7);
//
//
//    List<Integer> newData;
//
//    newData = a.subList(1,3);
//
//    for(Integer i:newData)
//        System.out.print(i+" ");
//    Integer i = Integer.valueOf(10000);
//    if(i==10000){
//        System.out.println(i);
//    }
    DecimalFormat df2 =new DecimalFormat("#");
    System.out.println(df2.format(3.54));

}

}
